﻿/*
Copyright (c) 2007-2011 William POTTIER - AllProgrammic

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
of the Software, and to permit persons to whom the Software is furnished to do so, 
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Net;
using NetAccess.Common;
using NetAccess.Common.Exceptions;

namespace NetAccess.Client
{
    /// <summary>
    /// Client Configuration class
    /// </summary>
    public class ClientConfiguration : NetConfiguration, ICloneable
    {
        #region ICloneable Members

        public object Clone()
        {
            return MemberwiseClone();
        }

        #endregion

        #region Fields
        private TimeSpan _discoverTimeout;

        private TimeSpan _connectTimeout;

        private string _serverRsaPublicKey;
        #endregion

        #region Properties
        /// <summary>
        /// Timeout de la découverte des services en milliseconde
        /// </summary>
        public TimeSpan DiscoverTimeout
        {
            get
            { return _discoverTimeout; }
            set
            {
                if (Locked) return;
                _discoverTimeout = value;
            }
        }

        /// <summary>
        /// Connection timeout
        /// </summary>
        public TimeSpan ConnectTimeout
        {
            get
            { return _connectTimeout; }
            set
            {
                if (Locked) return;
                _connectTimeout = value;
            }
        }

        /// <summary>
        /// Server public key for RSA encryption
        /// </summary>
        public string ServerPublicKeyString
        {
            get{ return _serverRsaPublicKey; }
            set
            {
                if (Locked) return;
                _serverRsaPublicKey = value;
            }
        }        
        #endregion


        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="appId">Application identifier</param>
        /// <param name="ip">Server IP address or hotstname</param>
        /// <param name="tcpServicePort">TCP service port number</param>
        /// <param name="udpServicePort">UDP service port number</param>
        public ClientConfiguration(string appId, IPAddress ip, int tcpServicePort, int udpServicePort)
        {
            ApplicationIdentifier = appId;
            DiscoverTimeout = TimeSpan.FromSeconds(10);
            ServiceDiscoveryIP = IPAddress.Broadcast;
            ConnectTimeout = TimeSpan.FromSeconds(15);
            TCPServicePort = tcpServicePort;
            UDPServicePort = udpServicePort;
            IP = ip;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="appId">Application identifier</param>
        /// <param name="ip">Server IP address or hotstname</param>
        /// <param name="tcpServicePort">TCP service port number</param>
        /// <param name="udpServicePort">UDP service port number</param>
        /// <exception cref="NetAccessException"></exception>
        public ClientConfiguration(string appId, string ip, int tcpServicePort, int udpServicePort)
        {
            ApplicationIdentifier = appId;
            DiscoverTimeout = TimeSpan.FromSeconds(10);
            ServiceDiscoveryIP = IPAddress.Broadcast;
            ConnectTimeout = TimeSpan.FromSeconds(15);
            TCPServicePort = tcpServicePort;
            UDPServicePort = udpServicePort;

            // Try to resolve hostname
            try
            {
                IPAddress ipaddress;
                if (!IPAddress.TryParse(ip, out ipaddress))
                {
                    var adresses = Dns.GetHostAddresses(ip);
                    if (adresses.Length > 0)
                        ipaddress = adresses[0];
                    else
                        throw new NetAccessException("Unable to resolve IP adresse");
                }

                IP = ipaddress;
            }
            catch (Exception ex)
            {
                throw new NetAccessException("Unable to resolve IP adresse. See InnerException for more informations",
                                             ex);
            }
        }
    }
}